package com.yuelife.market.controller;

import com.yuelife.market.entity.*;
import com.yuelife.market.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "ProductController")
@RestController
@RequestMapping("/pro")
@CrossOrigin//解决跨域的注解  但是只能局部 全局需要在启动类中写个bean
public class ProductController {

    @Autowired
    private MCategoryService mCategoryService;
    @Autowired
    private MProductService mProductService;
    @Autowired
    private MProductUserLikeService mProductUserLikeService;
    @Autowired
    private MCommentService mCommentService;
    @Autowired
    private MImgService mImgService;

    /**
     * 根据条件查询的
     * 不走redis
     * @param goodsCondition
     * @return
     */
    @ApiOperation(value = "根据条件(价格排序，商品名搜索)查询商品列表",notes = "展示条件查询的商品列表")
    @GetMapping("/conditionData")//Get请求
    public List<MProduct> conditionData(@ApiParam(name = "goodsCondition",value = "根据商品价格、名称添加展示商品列表",required=false)GoodsCondition goodsCondition){
        System.out.println(goodsCondition.getNormal()+"我是啥？");
        System.out.println(goodsCondition.getGoodsCollection()+"我是那个默认的值");
        System.out.println(goodsCondition.getPriceOrder()+"我是价格排序");
        System.out.println(goodsCondition.getSearchName()+"我是搜索内容");
        //查询所有商品
        return mProductService.conditionData(goodsCondition);
    }
    /**
     * 展现所有商品列表
     * @return
     */
    @ApiOperation(value = "展示所有商品列表")
    @GetMapping("/product")//Get请求
    public Map selectAllProduct(GoodsCondition goodsCondition){
        //查询所有商品
        return mProductService.selectAllProduct(goodsCondition);
    }




    /**
     * 商品个数
     */
    @ApiOperation(value = "得到商品个数",notes = "商品个数详情")
    @GetMapping("/totalCount")
    public Map selectProductCount(){
        Map map=new HashMap();
        Long count= null;
        try {
            count = mProductService.selectCount();
            map.put("data",count);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data","商品个数查询失败");
            map.put("code",500);
            return map;
        }

    }


    /**
     * 搜索功能
     * 价格排序
     * @return
     */
    @ApiOperation(value = "搜索功能，价格排序")
    @GetMapping("/searchProduct")
    public List<MProduct> selectProductBySearch(@ApiParam(name = "MProduct对象",value = "传入数据",required=false)MProduct mProduct){
        return mProductService.selectProductBySearch(mProduct);
    }

    /**
     * 点击商品过后，查看到商品详情
     * 我们是根据商品编号来的查看的
     * @return
     */
    @ApiOperation(value = "查看商品详情")
    @ApiImplicitParam(paramType = "path", name = "productSn", value = "商品编号", required = true, dataType = "String")
    @GetMapping("/detailProduct/{goodsId}")
     public MProduct selectProductBySn(@PathVariable("goodsId") String productSn){
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.addObject("detailPoduct",mProductService.selectProductBySn(productSn));
//        return modelAndView;
       return mProductService.selectProductBySn(productSn);
    }

    /**
     * 点击商品详情过后，加载商品所有图片
     * @param productSn
     * @return
     */
    @ApiOperation(value = "查询商品所有的图片")
    @ApiImplicitParam(paramType = "path", name = "productSn", value = "商品编号", required = true, dataType = "String")
    @GetMapping("/selectManyImg/{goodsId}")
    public Map selectManyImg(@PathVariable("goodsId") String productSn){
        Map map =new HashMap();
        try {
            List<String> imgSrc=mImgService.selectManyImg(productSn);
            map.put("code",200);
            map.put("data",imgSrc);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }


    /**
     * 进入商品详情页的时候，显示留言或者评论数据
     * @param productSn
     * @return
     */
    @ApiOperation(value = "查询商品评论列表")
    @ApiImplicitParam(paramType = "path", name = "productSn", value = "商品编号", required = true, dataType = "String")
    @GetMapping("/commentList/{goodsId}")
    public Map selectComment(@PathVariable("goodsId")String productSn){
        Map map=new HashMap();
        try {
            List<MComment> mComments=mCommentService.selectComment(productSn);
            map.put("data",mComments);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data","查询出错");
            map.put("code",500);
            return map;
        }
    }


    /**
     * 增加商品评论
     * @return
     */
    @ApiOperation(value = "买家添加评论")
    @PostMapping("/addComment")
    public int addComment(@ApiParam(name = "MComment",value = "MComment对象",required=true)MComment mComment){
        System.out.println("我又是啥评论："+mComment.getContent());
        System.out.println("我又是啥用户id：是不是null？没有？没有就对了"+mComment.getUserSn());
        System.out.println("我是啥呢？是不是商品id："+mComment.getProductSn());
        try {
            mCommentService.insert(mComment);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

    /**
     * 卖家回复
     * @param mComment
     * @return
     */
    @ApiOperation(value = "卖家回复")
    @PostMapping("/addReply")
    public int addReply(@ApiParam(name = "MComment",value = "MComment对象",required=true)MComment mComment){
//        System.out.println("我是回复："+mComment.getReply());
//        System.out.println("我是id"+mComment.getCid());
        try {
            mCommentService.addReply(mComment);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

    /**
     * 初始化是否收藏，就是商品详情页的时候，收藏是否已被用户收藏或点赞过
     * @param productSn
     * @param userSn
     * @return
     */
    @ApiOperation(value = "查看该用户是否有收藏该商品")
    @GetMapping("/isFavorite")
    public Map isFavorite(@ApiParam(name = "productSn",value = "productSn商品编号",required=true)String productSn,@ApiParam(name = "userSn",value = "userSn用户编号",required=true)String userSn){
        System.out.println("初始化收藏");
        System.out.println("我是用户编号："+userSn);
        System.out.println("我是商品编号:"+productSn);
        Map map=new HashMap();
        try {
            Boolean status=mProductUserLikeService.selectFavorite(productSn,userSn);
            map.put("data",status);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data","收藏状态错误");
            map.put("code",500);
            return map;
        }
    }


    /**
     * 收藏
     * 商品点赞数+1
     * 商品用户点赞中间表插入一条数据（方便用户中心查看自己点赞的商品）
     * @return
     */
    @ApiOperation(value = "用户收藏商品")
    @PostMapping("/addFavorite")
    public int userLikeProduct(@ApiParam(name = "mProductUserLike",value = "mProductUserLike对象",required=true)MProductUserLike mProductUserLike){
        System.out.println("点赞");
        System.out.println(mProductUserLike.getUserSn());
        System.out.println("ss"+mProductUserLike.getProductSn());
        try {
            //商品表中的收藏数增加
            mProductService.addlikes(mProductUserLike.getProductSn());
            //往用户商品收藏中间表中插入数据
            mProductUserLikeService.insert(mProductUserLike);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

    /**
     * 取消收藏  就需要在中间表删除原有点赞的，然后商品中的收藏数量减 1
     * @param mProductUserLike
     * @return
     */
    @ApiOperation(value = "用户取消收藏商品")
    @DeleteMapping("/noFavorite")
    public int noFavorite(@ApiParam(name = "mProductUserLike",value = "mProductUserLike对象",required=true)MProductUserLike mProductUserLike){
        System.out.println("取消点赞");
        System.out.println(mProductUserLike.getUserSn());
        System.out.println("ss"+mProductUserLike.getProductSn());
        //得到商品编号
        String productSn=mProductUserLike.getProductSn();
        try {
            //商品表收藏数减 1
            mProductService.reduceLikes(productSn);
            //用户商品收藏中间表删除数据
            mProductUserLikeService.deleteFavorite(mProductUserLike);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }
    /**
     * 展现的分类，就是用户添加物品时，下拉菜单的分类
     * @return
     */
    @ApiOperation(value = "查询所有分类")
    @GetMapping("/category")
    public List<MCategory> selectAllCategory(){
        return mCategoryService.selectAll();
    }

    /**
     * 创建商品
     * 并且上传首页图片
     * @param mProduct
     * @param files
     * @return
     */
    @ApiOperation(value = "创建商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mProduct", value = "mProducts商品对象", required = true),
            @ApiImplicitParam(name = "files", value = "图片文件", required = true)
    })
    @PostMapping("/uploadImg")//需要返回后端创建的商品编号（UUID），以便之后上传多张图片时，会用到商品编号
    public Map uploadImg(MProduct mProduct , @RequestParam("files") MultipartFile files){
            //创建商品信息
        try {
            //会返回是否成功的状态码  200成功  500失败
            return mProductService.insert(mProduct,files);
        } catch (Exception e) {
            e.printStackTrace();
            Map map =new HashMap();
            map.put("code",500);
            return map;
        }
    }

    /**
     * 上传多张图片
     * 在前端循环上传的
     * @param productSn
     * @param manyFiles
     * @return
     */
    @ApiOperation(value = "上传多张图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productSn", value = "productSn商品编号", required = true),
            @ApiImplicitParam(name = "manyFiles", value = "manyFiles图片文件", required = true)
    })
    @PostMapping("/uploadManyImg")
    public int uploadManyImg(String productSn,@RequestParam("manyFiles") MultipartFile manyFiles){
            return mImgService.insert(productSn,manyFiles);
    }



    //这个是微服务通信的，当商品在后台被管理通过时，需要修改用户发布商品在redis中的状态
    @ApiOperation(value = "商品通过审核时，修改商品状态")
    @ApiImplicitParam(name = "mProduct", value = "mProduct商品对象，微服务的，json格式", required = true)
    @PostMapping("/updataRedisProduct")
    public void  updataRedisProduct(@RequestBody MProduct mProduct){
        System.out.println("我是微服务之间的调用");
        mProductService.updataRedisProduct(mProduct);
    }

    //微服务调用，由admin服务调用，当删除商品时，需要将redis中的商品数据也删除掉
    //因为admin中的删除是使用声名式注解的，我们在redis中的商品列表是由RedisTemplate添加的，所以不能使用声名式注解删除，只能通过以下方式删除，
    //redis中是Map的格式
    @ApiOperation(value = "根据商品编号删除商品")
    @ApiImplicitParam(name = "productSn", value = "productSn商品编号", required = true)
    @DeleteMapping("/deleteRedisProduct")
    public void deleteRedisProduct(String productSn){
        mProductService.deleteRedisProduct(productSn);
    }




}
